<?php

require_once 'domain/Denuncia.php';
require_once 'datatypes/DenunciaInfo.php';
require_once(APPPATH . 'libraries/MY_Model.php');

class M_denuncia extends MY_Model {

	function M_denuncia() {
		parent::MY_Model('denuncias', 'denuncia_id');
	}

	/*	 * ***************************************************
	 * ABSTRACT REQUIRED
	 * ************************************************** */

	protected function afromEntityToDb(Denuncia $den) {
		return array(
			"denuncia_id" => $den->id,
			"user_id" => $den->user_id,
			"den_date" => $this->getDateToDb($den->date),
			"den_obs" => $den->obs,
			"den_active" => $den->active,
			"suite_channel_id" => $den->suite_channel_id
		);
	}

	protected function afromDbToEntity(array $p_denuncia) {
		$d = new Denuncia();
		$d->id = $p_denuncia["denuncia_id"];
		$d->user_id = $p_denuncia["user_id"];
		$d->date = $this->getDateFromDb($p_denuncia["den_date"]);
		$d->obs = $p_denuncia["den_obs"];
		$d->active = $p_denuncia["den_active"];
		$d->suite_channel_id = $p_denuncia["suite_channel_id"];
		return $d;
	}

	/*	 * ***************************************************
	 * EXTRA FUNCTIONS
	 * ************************************************** */

	function getDenuncias($channel_id) {
		$this->db->select('denuncia_id, user_login as user, den_obs as descripcion');
		$this->db->from($this->TABLE_NAME);
		$this->db->join('pgc_users', 'pgc_denuncias.user_id = pgc_users.user_id');
		$this->db->where("suite_channel_id = $channel_id");

		$query = $this->db->get();

		$resultset = $query->result_array();

		$ret = array();
		foreach ($resultset as $row) {
			$den = new DenunciaInfo();

			$den->id = $row['denuncia_id'];
			$den->user_name = $row['user'];
			$den->description = $row['descripcion'];

			$ret[] = $den;
		}

		return array(
			"denuncias" => $ret,
			"channel_id" => $channel_id
		);
	}

	function create(Denuncia $denuncia) {
		$denuncia->date = date_create();
		$denuncia->active = true;
		return parent::create($denuncia);
	}

}

?>
